package com.twentyOne.ClothingLand.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.twentyOne.ClothingLand.service.ISaleOrderService;
import com.twentyOne.ClothingLand.service.impl.SaleOrderService;
import com.twentyOne.ClothingLand.service.impl.SalesReportService;
import com.twentyOne.ClothingLand.util.JsonUtil;
import com.twentyOne.ClothingLand.vo.Goods;

public class SalesReportAction extends ActionSupport {
	
	private String year;
	private String classId;
	private String startDate;
	private String endDate;
	private int shopId;
	private SalesReportService salesReportService = new SalesReportService();
	
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getClassId() {
		return classId;
	}
	public void setClassId(String classId) {
		this.classId = classId;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public int getShopId() {
		return shopId;
	}
	public void setShopId(int shopId) {
		this.shopId = shopId;
	}
	
	public String searchMonthlySalesReport() throws Exception {
		
		String amounts = "";
		for (int i = 1; i < 13; i++) {
			if(i == 12) {
				amounts += this.salesReportService.searchTotalByYM(year, (i + ""));
			}else {
				amounts += this.salesReportService.searchTotalByYM(year, (i + "")) + ",";
			}
		}
								
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("[{\"amounts\":\"" + amounts + "\"}]");

		return null;

	}
	
	public String searchClassifiedSalesReport() throws Exception {
		
		String[] clsaaIdStrings = classId.split(",");
		String amounts = "";
		for (int i = 0; i < clsaaIdStrings.length; i++) {
			if(i == (clsaaIdStrings.length - 1)) {
				amounts += this.salesReportService.searchTotalByClass_YMD(clsaaIdStrings[i], startDate, endDate, shopId);
			}else {
				amounts += this.salesReportService.searchTotalByClass_YMD(clsaaIdStrings[i], startDate, endDate, shopId) + ",";
			}
			
			
		}
				
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("[{\"amounts\":\"" + amounts + "\"}]");
		
		return null;
		
	}
	
}
